﻿namespace EFS.Common.UI.Controls
{
	public partial class ImageButton : System.Windows.Controls.Button
	{
		/// eButtonState
		private enum eButtonState
		{
			Hot,
			Normal,
			Pressed
		}

		/// button state
		private eButtonState buttonState = eButtonState.Normal;

		/// timer wait
		private System.Int32 timerClipCurrent = 0;
		private System.Windows.Threading.DispatcherTimer timerWait = null;

		/// wait border colors
		private System.Boolean waitColorSwitch = false;
		private System.Windows.Media.Brush waitInitialBorderBrush = null;

		public ImageButton() : base()
		{
			InitializeComponent();

			// set initial
			this.Width = 24;
			this.Height = 24;
			this.UseWait = false;
			this.waitInitialBorderBrush = this.BorderBrush;
			this.WaitBorderColor1 = System.Windows.Media.Colors.Yellow;
			this.WaitBorderColor2 = System.Windows.Media.Colors.Magenta;

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
			{
				this.Loaded += new System.Windows.RoutedEventHandler(ImageButton_Loaded);
				this.Unloaded += new System.Windows.RoutedEventHandler(ImageButton_Unloaded);
			}
		}

		#region Properties
		/// Property ImageEnabled: Get, Set
		private System.Windows.Media.ImageSource imageNormal = null;
		public System.Windows.Media.ImageSource ImageNormal
		{
			get
			{
				return imageNormal;
			}
			set
			{
				if (imageNormal != value)
				{
					imageNormal = value;
					UpdateButton();
				}
			}
		}

		/// Property ImageDisabled: Get, Set
		private System.Windows.Media.ImageSource imageDisabled = null;
		public System.Windows.Media.ImageSource ImageDisabled
		{
			get
			{
				return imageDisabled;
			}
			set
			{
				if (imageDisabled != value)
				{
					imageDisabled = value;
					UpdateButton();
				}
			}
		}

		/// Property ImageHot: Get, Set
		private System.Windows.Media.ImageSource imageHot = null;
		public System.Windows.Media.ImageSource ImageHot
		{
			get
			{
				return imageHot;
			}
			set
			{
				if (imageHot != value)
				{
					imageHot = value;
					UpdateButton();
				}
			}
		}

		/// Property ImagesWait: Get, Set
		//public System.Windows.Media.ImageSource[] ImagesWait { get; set; }

		/// Property WaitBorderColor1: Get, Set
		public System.Windows.Media.Color WaitBorderColor1 { get; set; }

		/// Property WaitBorderColor2: Get, Set
		public System.Windows.Media.Color WaitBorderColor2 { get; set; }

		/// UseWait
		public System.Boolean UseWait { get; set; }

		/// Property IsEnabled: Get, Set
		public new System.Boolean IsEnabled
		{
			get
			{
				return base.IsEnabled;
			}
			set
			{
				if (base.IsEnabled != value)
				{
					base.IsEnabled = value;
					UpdateButton();
				}
			}
		}

		// PushButton State Property
		private eButtonState ButtonState
		{
			get
			{
				return buttonState;
			}
			set
			{
				if (buttonState != value)
				{
					buttonState = value;
					UpdateButton();
				}
			}
		}

		/// Property Image: Get
		private System.Windows.Media.ImageSource Image
		{
			get
			{
				// default
				System.Windows.Media.ImageSource retValue = ImageDisabled;

				// only if enabled
				if (this.IsEnabled)
				{
					// switch images
					switch (buttonState)
					{
						default:
						case eButtonState.Normal:
							{
								retValue = ImageNormal;
								break;
							}
						case eButtonState.Hot:
							{
								retValue = ImageHot;
								break;
							}
						case eButtonState.Pressed:
							{
								retValue = ImageHot;
								break;
							}
					}
				}

				// ok
				return retValue;
			}
		}

		// UpdateButton
		private void UpdateButton()
		{
			image.Source = Image;
		}
		#endregion

		/// StartWait
		private void StartWait()
		{
			// stop waiting
			StopWait();

			timerClipCurrent = 0;
			timerWait = new System.Windows.Threading.DispatcherTimer();
			timerWait.Interval = new System.TimeSpan(0, 0, 0, 0, 1000);
			timerWait.Tick += new System.EventHandler(timerWait_Tick);
			timerWait.Start();
		}

		/// Wait sequence
		private void timerWait_Tick(object sender, System.EventArgs e)
		{
			++timerClipCurrent;
			if (timerClipCurrent > 4)
			{
				StopWait();
			}

			// get next image
			if (/*EFS.Common.Verify.Array(ImagesWait) &&*/ null != timerWait && timerWait.IsEnabled)
			{
				System.Windows.Media.Brush newBorderBrush = new System.Windows.Media.SolidColorBrush(WaitBorderColor1);
				if (waitColorSwitch)
				{
					waitColorSwitch = !waitColorSwitch;
					newBorderBrush = new System.Windows.Media.SolidColorBrush(WaitBorderColor2);
				}

				// set
				this.BorderBrush = newBorderBrush;
			}
		}

		/// StopWait
		public void StopWait()
		{
			// stop waiting
			if (null != timerWait && timerWait.IsEnabled)
			{
				timerWait.Stop();
				timerWait = null;
			}

			// set brush
			this.BorderBrush = waitInitialBorderBrush;
		}

		#region Events
		/// Loaded event
		private void ImageButton_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			UpdateButton();
		}

		/// Unloaded
		private void ImageButton_Unloaded(object sender, System.Windows.RoutedEventArgs e)
		{
			StopWait();
		}

		/// OnMouseLeftButtonDown
		protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
		{
			// call base
			base.OnMouseLeftButtonDown(e);

			// set
			ButtonState = eButtonState.Pressed;
		}

		/// OnMouseLeftButtonUp
		protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
		{
			// call base
			base.OnMouseLeftButtonUp(e);

			// get point
			System.Windows.Point point = e.GetPosition(this);
			if (point.X >= 0 && point.X <= this.Width &&
				point.Y >= 0 && point.Y <= this.Height)
			{
				return;
			}

			// set
			ButtonState = eButtonState.Normal;
		}

		/// OnMouseEnter
		protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
		{
			// call base
			base.OnMouseEnter(e);

			// set
			ButtonState = eButtonState.Hot;
		}

		/// OnMouseLeave
		protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
		{
			// call base
			base.OnMouseLeave(e);

			// set
			ButtonState = eButtonState.Normal;
		}

		/// OnClick
		protected override void OnClick()
		{
			// call base
			base.OnClick();

			// start wait
			if (UseWait)
			{
				StartWait();
			}
		}
		#endregion
	}

}